//
//  YFGoodsTypeView.m
//  QuanJieShopProject
//
//  Created by apple on 2018/5/5.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YFGoodsTypeView.h"
#import "UICollectionViewLeftAlignedLayout.h"
#import "YFSelectedTagView.h"

#import "CollectionViewCell.h"

@interface YFGoodsTypeView ()
{
    
    UIImageView *_imgView;
    UILabel *_priceLabel;
    UILabel *_stockLabel;
}
@property (nonatomic, strong) UIView *showView;
@property (nonatomic, strong) UIView *coverView;

@property (nonatomic, strong) YFSelectedTagView *selectedTagView;

@end
@implementation YFGoodsTypeView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.hidden = YES;
        //        [self makeView];
    }
    return self;
}

- (void)show {
    //    NSLog(@"goodsTypeArr = %@", self.goodsTypeArr);
    //    NSLog(@"selectedArr = %@", self.selectedArr);
    //    NSLog(@"dataSourceArr = %@", self.selectedTagView.dataSourceArr);
    
    self.hidden = NO;
    [self addSubview:self.coverView];
    [self addSubview:self.showView];
    
    //    self.selectedTagView.titleArr = self.titleArr;
    //    self.selectedTagView.dataSourceArr = self.dataSourceArr;
}

- (void)hidden {
    self.hidden = YES;
    [self.coverView removeFromSuperview];
    [self.showView removeFromSuperview];
}

- (void)setGoodsPriceArr:(NSArray<YFGoodsPriceModel *> *)goodsPriceArr {
    _goodsPriceArr = goodsPriceArr;
    
    self.selectedTagView.goodsPriceArr = goodsPriceArr;
}


- (void)setGoodsTypeArr:(NSArray <YFGoodsTypeModel *>*)goodsTypeArr {
    _goodsTypeArr = goodsTypeArr;
    
    self.selectedTagView.goodsTypeArr = goodsTypeArr;
}

- (void)confirmBtnClick {
    //    NSLog(@"selectedArr = %@", self.selectedArr);
    // 选择数目跟展示数目不同, 就证明有没有选择的规格
    // 可以提示具体哪些忘记选择了
    
    NSLog(@"selectedArr = %lu", (unsigned long)self.selectedArr.count);
    NSLog(@"goodsTypeArr = %lu", (unsigned long)self.goodsTypeArr.count);
    
    if (self.selectedArr.count != self.goodsTypeArr.count) {
        [MBPHUD showAlertWithTitle:@"规格信息有漏选,请选择!"];
    }else {
        
        if (self.confirmBlock) {
            self.confirmBlock(self.model, self.selectedArr, self.selectedTagView.count);
        }
        [self hidden];
    }
}

- (UIView *)showView {
    if (!_showView) {
        // 展示内容
        CGFloat showViewH = self.bounds.size.height * 0.6;
        CGFloat showViewY = self.bounds.size.height - showViewH;
        UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, showViewY, self.bounds.size.width, showViewH)];
        showView.backgroundColor = [UIColor whiteColor];
        [self addSubview:showView];
        self.showView = showView;
        
        { // 具体内容
            // 确定按钮
            CGFloat confirmBtnH = 54;
            CGFloat confirmBtnY = self.showView.frame.size.height - confirmBtnH;
            UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, confirmBtnY, self.showView.frame.size.width, confirmBtnH)];
            confirmBtn.backgroundColor = YFColor_Red;
            [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [confirmBtn setTitle:@"确 定" forState:UIControlStateNormal];
            [confirmBtn addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
            [self.showView addSubview:confirmBtn];
            
            // title
            //            CGFloat titleH = 40;
            //            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.showView.frame.size.width, titleH)];
            //            titleLabel.textAlignment = NSTextAlignmentCenter;
            //            titleLabel.backgroundColor = [UIColor whiteColor];
            //            titleLabel.text = @"规格选择";
            //            [self.showView addSubview:titleLabel];
            
            // 图片
            CGFloat imgWH = 210.f/750.f * UISCREEN_WIDTH;
            _imgView = [[UIImageView alloc] init];
            _imgView.image = [UIImage imageNamed:@"banner5"];
            _imgView.frame = CGRectMake(10, 10, imgWH, imgWH);
            _imgView.backgroundColor = [UIColor blueColor];
            _imgView.layer.cornerRadius = 6;
            [self.showView addSubview:_imgView];
            
            
            // 价格
            CGFloat priceX = CGRectGetMaxX(_imgView.frame) + 10;
            CGFloat priceY = 10;
            _priceLabel = [[UILabel alloc] init];
            _priceLabel.frame = CGRectMake(priceX, priceY, 150, 20);
            _priceLabel.text = @"¥1222.22";
            _priceLabel.textColor = YFColor_Red;
            _priceLabel.font = kFont32px;
            [self.showView addSubview:_priceLabel];
            
            // 库存 stockLabel;
            CGFloat stockLabelX = priceX;
            CGFloat stockLabelY = CGRectGetMaxY(_priceLabel.frame) + 10;
            _stockLabel = [[UILabel alloc] init];
            _stockLabel.frame = CGRectMake(stockLabelX, stockLabelY, 150, 20);
            _stockLabel.text = @"库存102件";
            _stockLabel.textColor = YFColor_Text;
            _stockLabel.font = kFont22px;
            [self.showView addSubview:_stockLabel];
            
            // selectedTagView
            CGFloat collectViewH = self.showView.frame.size.height - imgWH - confirmBtnH;
            UICollectionViewLeftAlignedLayout * layout = [[UICollectionViewLeftAlignedLayout alloc] init];
            CGRect collectionViewFrame = CGRectMake(0, CGRectGetMaxY(_imgView.frame)+10, self.showView.frame.size.width, collectViewH);
            self.selectedTagView = [[YFSelectedTagView alloc] initWithFrame:collectionViewFrame collectionViewLayout:layout];
            kWeakSelf;
            
            self.selectedTagView.didSelectedBlock = ^(YFGoodsPriceModel *model, NSArray *selectedArr) {
                
                NSLog(@"马上刷新界面");
                NSLog(@"price=%@, count=%@",  model.price, model.store_count);
                NSLog(@"count = %ld", (long)weakSelf.selectedTagView.count);
                _priceLabel.text = [NSString stringWithFormat:@"¥%@", model.price];
                _stockLabel.text = [NSString stringWithFormat:@"库存%@件", model.store_count];
                
                // 传这个数组, 主要是为了"确定"按钮判断
                weakSelf.selectedArr = [NSArray arrayWithArray:selectedArr];
                weakSelf.model = model;
            };
            [self.showView addSubview:self.selectedTagView];
        }
    }
    return _showView;
}

- (YFSelectedTagView *)selectedTagView {
    if (!_selectedTagView) {
        self.showView; // 这里直接调用懒加载就可以了
    }
    return _selectedTagView;
}

- (UIView *)coverView {
    if (!_coverView) {
        // 遮盖
        UIView *coverView = [[UIView alloc] initWithFrame:self.bounds];
        coverView.backgroundColor = [UIColor blackColor];
        coverView.alpha = 0.3;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidden)];
        [coverView addGestureRecognizer:tap];
        _coverView = coverView;
    }
    return _coverView;
}
- (void)detailTypeUpdateWithDic:(NSDictionary *)dic{
    
    [_imgView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"original_img"]]];
    _priceLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"shop_price"]];
    _stockLabel.text = [NSString stringWithFormat:@"库存:%@",[dic objectForKey:@"store_count"]];
}

@end

